package cn.iocoder.yudao.module.trade.service.ordertransfer;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.trade.controller.admin.ordertransfer.vo.OrderTransferPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.ordertransfer.vo.OrderTransferSaveReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordertransfer.OrderTransferDO;
import cn.iocoder.yudao.module.trade.dal.mysql.ordertransfer.OrderTransferMapper;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderOperateTypeEnum;
import cn.iocoder.yudao.module.trade.framework.order.core.annotations.TradeOrderLog;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.ORDER_TRANSFER_NOT_EXISTS;

/**
 * 订单转派记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class OrderTransferServiceImpl implements OrderTransferService {

    @Resource
    private OrderTransferMapper orderTransferMapper;
    @TradeOrderLog(operateType = TradeOrderOperateTypeEnum.SYSTEM_TRANSFER)
    @Override
    public Long createOrderTransfer(OrderTransferSaveReqVO createReqVO) {
        // 插入
        OrderTransferDO orderTransfer = BeanUtils.toBean(createReqVO, OrderTransferDO.class);
        orderTransferMapper.insert(orderTransfer);
        // 返回
        return orderTransfer.getId();
    }

    @Override
    public void updateOrderTransfer(OrderTransferSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderTransferExists(updateReqVO.getId());
        // 更新
        OrderTransferDO updateObj = BeanUtils.toBean(updateReqVO, OrderTransferDO.class);
        orderTransferMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderTransfer(Long id) {
        // 校验存在
        validateOrderTransferExists(id);
        // 删除
        orderTransferMapper.deleteById(id);
    }

    private void validateOrderTransferExists(Long id) {
        if (orderTransferMapper.selectById(id) == null) {
            throw exception(ORDER_TRANSFER_NOT_EXISTS);
        }
    }

    @Override
    public OrderTransferDO getOrderTransfer(Long id) {
        return orderTransferMapper.selectById(id);
    }

    @Override
    public PageResult<OrderTransferDO> getOrderTransferPage(OrderTransferPageReqVO pageReqVO) {
        return orderTransferMapper.selectPage(pageReqVO);
    }

}